package com.audaque.springboot.foshanupload.web.nosaveframeworkloginvalid.config;


import com.audaque.springboot.foshanupload.authcore.properties.LoginProperties;
import com.audaque.springboot.foshanupload.authcore.properties.SessionJwtOrderProperties;
import com.audaque.springboot.foshanupload.core.constants.ComConst;
import com.audaque.springboot.foshanupload.web.properties.IgnoreResourcePathProperties;

import com.audaque.springboot.foshanupload.web.nosaveframeworkloginvalid.interceptor.NeedLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zgb
 * @desc Interceptor用于鉴证和鉴权
 * @date 2022-01-25 22:24:15
 */
@Configuration
public class LoginValidInterceptorConfig implements WebMvcConfigurer {

    @Autowired
    private IgnoreResourcePathProperties ignoreResourcePathProperties;

    @Autowired
    private NeedLoginInterceptor needLoginInterceptor;
    @Autowired
    private SessionJwtOrderProperties sessionJwtOrderProperties;
    @Autowired
    private LoginProperties loginProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {


        //表示要拦截
        String[] addPathPatterns = {
                ComConst.INTERCEPTOR_ANY_MATCH
        };


        //要排除的路径
        List<String> excludedUris = new ArrayList<>();
        List<String> excludePathPatterns= ignoreResourcePathProperties.getList();
        List<String> unAuthenticatedUrlList = loginProperties.getUnAuthenticatedUrlList();
        excludedUris.addAll(excludePathPatterns);
        excludedUris.addAll(unAuthenticatedUrlList);
        //用注解识别是否需要登录，而不是路径
        registry.addInterceptor(needLoginInterceptor).addPathPatterns(addPathPatterns).excludePathPatterns(excludedUris).order(sessionJwtOrderProperties.getNeedLoginInterceptor());


    }


}
